# 自定义过滤器,实现根据传入的参数,返回指定的字符串
from flask import current_app
from flask import g
from flask import session
from info.models import User



def index_class(index):
    if index == 0:
        return 'first'
    elif index == 1:
        return 'second'
    elif index == 2:
        return 'third'
    else:
        return ''

# 检查用户是否登陆,装饰器实现
def login_required(f):
    def wrapper(*args,**kwargs):
        # 尝试获取用户的登陆信息
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 使用g对象来保护用户信息
        g.user = user
        return f(*args,**kwargs)
    # 让被装饰的函数名称在返回wapper之前赋值给wapper的__name__
    wrapper.__name__=f.__name__
    return wrapper